<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CPP11ThreadPool: workstealingpoolexecutor.hpp 源文件</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="CPP.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">CPP11ThreadPool
   </div>
   <div id="projectbrief">C++11线程池</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'搜索');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','搜索');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">workstealingpoolexecutor.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef WORKSTEALINGPOOLEXECUTOR_H</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#define WORKSTEALINGPOOLEXECUTOR_H</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &quot;threadpoolexecutor.hpp&quot;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;</div><div class="line"><a name="l00009"></a><span class="lineno"><a class="line" href="classWorkStealingPoolExecutor.html">    9</a></span>&#160;<span class="keyword">class </span><a class="code" href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a>: <span class="keyword">public</span> <a class="code" href="classThreadPoolExecutor.html">ThreadPoolExecutor</a> {</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;    <span class="keyword">public</span>:</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;        <a class="code" href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a>(int32_t corePoolSize,</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;                                 int32_t maxPoolSize,</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;                                 <span class="keyword">const</span> std::vector&lt;<a class="code" href="classBlockingQueue.html">BlockingQueue&lt;Runnable::sptr&gt;</a>&gt;&amp; workQueue,</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;                                 <span class="keyword">const</span> <a class="code" href="classRejectedExecutionHandler.html">RejectedExecutionHandler</a>&amp; handler,</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;                                 <span class="keyword">const</span> std::string&amp; prefix = <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;        <a class="code" href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a>(int32_t corePoolSize,</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;                                 int32_t maxPoolSize,</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;                                 <span class="keyword">const</span> std::vector&lt;<a class="code" href="classBlockingQueue.html">BlockingQueue&lt;Runnable::sptr&gt;</a>&gt;&amp; workQueue,</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;                                 <a class="code" href="classRejectedExecutionHandler.html">RejectedExecutionHandler</a>* handler,</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;                                 <span class="keyword">const</span> std::string&amp; prefix = <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;        <a class="code" href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a>(int32_t corePoolSize,</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;                                 int32_t maxPoolSize,</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;                                 <span class="keyword">const</span> std::string&amp; prefix = <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        ~<a class="code" href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a>() {}</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;    <span class="keyword">public</span>:</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> F&gt;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;        std::future&lt;typename std::result_of&lt;F()&gt;::type&gt;</div><div class="line"><a name="l00043"></a><span class="lineno"><a class="line" href="classWorkStealingPoolExecutor.html#a84571339d5bafe81925c10d27f020ab4">   43</a></span>&#160;        <a class="code" href="classWorkStealingPoolExecutor.html#a84571339d5bafe81925c10d27f020ab4">submit</a>(F f, <span class="keywordtype">bool</span> core = <span class="keyword">true</span>) {</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;            <span class="keyword">using</span> result_type = <span class="keyword">typename</span> std::result_of&lt;F()&gt;::type;</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;            std::packaged_task&lt;result_type()&gt; task(std::move(f));</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;            std::future&lt;result_type&gt; res(task.get_future());</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;            <span class="keywordflow">if</span>(<a class="code" href="classThreadPoolExecutor.html#aec8e7c250025db138cde555d759c8194">addWorker</a>(<a class="code" href="classRunnable.html">Runnable</a>(std::move(task)), core)) {</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;                <span class="keywordflow">return</span> res;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;                <span class="keywordtype">int</span> c = ctl_.load();</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;                <span class="keywordflow">if</span> (!<a class="code" href="classThreadPoolExecutor.html#a841ae2189d6f85b9df9f69dec5375341">isRunning</a>(c)) {</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;                    <a class="code" href="classThreadPoolExecutor.html#a14c4e3d786dcf22e858b11a95d2f77ef">reject</a>(<a class="code" href="classRunnable.html">Runnable</a>(std::move(task)));</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;                    <span class="keywordflow">return</span> res;</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;                }</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;            }</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;        }</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;        <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classWorkStealingPoolExecutor.html#ad00399d153ba5d77704dae2c1816b566">workerThread</a>(<span class="keywordtype">size_t</span> queueIdex) <span class="keyword">override</span>;</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;        <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classWorkStealingPoolExecutor.html#ae0445ee114ceec03cf9513c394df7c89">coreWorkerThread</a>(<span class="keywordtype">size_t</span> queueIdex) <span class="keyword">override</span>;</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;};</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;<span class="preprocessor">#endif </span><span class="comment">/* WORKSTEALINGPOOLEXECUTOR_H */</span><span class="preprocessor"></span></div><div class="ttc" id="classWorkStealingPoolExecutor_html_a84571339d5bafe81925c10d27f020ab4"><div class="ttname"><a href="classWorkStealingPoolExecutor.html#a84571339d5bafe81925c10d27f020ab4">WorkStealingPoolExecutor::submit</a></div><div class="ttdeci">std::future&lt; typename std::result_of&lt; F()&gt;::type &gt; submit(F f, bool core=true)</div><div class="ttdoc">submit 在将来某个时候执行给定的任务, 任务可以在新线程或现有的合并的线程中执行, 可以有返回值,向任务队列提交的是任务副本 会抛出异常 </div><div class="ttdef"><b>Definition:</b> workstealingpoolexecutor.hpp:43</div></div>
<div class="ttc" id="classThreadPoolExecutor_html_aec8e7c250025db138cde555d759c8194"><div class="ttname"><a href="classThreadPoolExecutor.html#aec8e7c250025db138cde555d759c8194">ThreadPoolExecutor::addWorker</a></div><div class="ttdeci">virtual bool addWorker(Runnable task, bool core=true) final</div><div class="ttdoc">addWorker 将任务添加到队列 </div></div>
<div class="ttc" id="classThreadPoolExecutor_html_a841ae2189d6f85b9df9f69dec5375341"><div class="ttname"><a href="classThreadPoolExecutor.html#a841ae2189d6f85b9df9f69dec5375341">ThreadPoolExecutor::isRunning</a></div><div class="ttdeci">virtual bool isRunning(int c) const final</div><div class="ttdoc">isRunning 是否还在运行 </div><div class="ttdef"><b>Definition:</b> threadpoolexecutor.hpp:408</div></div>
<div class="ttc" id="classWorkStealingPoolExecutor_html_ae0445ee114ceec03cf9513c394df7c89"><div class="ttname"><a href="classWorkStealingPoolExecutor.html#ae0445ee114ceec03cf9513c394df7c89">WorkStealingPoolExecutor::coreWorkerThread</a></div><div class="ttdeci">virtual void coreWorkerThread(size_t queueIdex) override</div><div class="ttdoc">workerThread 非核心工作线程 </div></div>
<div class="ttc" id="classRejectedExecutionHandler_html"><div class="ttname"><a href="classRejectedExecutionHandler.html">RejectedExecutionHandler</a></div><div class="ttdoc">不再接受任务时的拒绝策略 </div><div class="ttdef"><b>Definition:</b> threadpoolexecutor.hpp:17</div></div>
<div class="ttc" id="classThreadPoolExecutor_html"><div class="ttname"><a href="classThreadPoolExecutor.html">ThreadPoolExecutor</a></div><div class="ttdoc">线程池基本实现,每个线程都有一个任务队列 </div><div class="ttdef"><b>Definition:</b> threadpoolexecutor.hpp:35</div></div>
<div class="ttc" id="classRunnable_html"><div class="ttname"><a href="classRunnable.html">Runnable</a></div><div class="ttdoc">Runnable interface 重写operator() </div><div class="ttdef"><b>Definition:</b> runnable.hpp:9</div></div>
<div class="ttc" id="classWorkStealingPoolExecutor_html_ad00399d153ba5d77704dae2c1816b566"><div class="ttname"><a href="classWorkStealingPoolExecutor.html#ad00399d153ba5d77704dae2c1816b566">WorkStealingPoolExecutor::workerThread</a></div><div class="ttdeci">virtual void workerThread(size_t queueIdex) override</div><div class="ttdoc">workerThread 核心工作线程 </div></div>
<div class="ttc" id="classBlockingQueue_html"><div class="ttname"><a href="classBlockingQueue.html">BlockingQueue</a></div><div class="ttdoc">BlockingQueue 阻塞队列(FIFO) </div><div class="ttdef"><b>Definition:</b> blockingqueue.hpp:14</div></div>
<div class="ttc" id="classThreadPoolExecutor_html_a14c4e3d786dcf22e858b11a95d2f77ef"><div class="ttname"><a href="classThreadPoolExecutor.html#a14c4e3d786dcf22e858b11a95d2f77ef">ThreadPoolExecutor::reject</a></div><div class="ttdeci">virtual void reject(const Runnable &amp;command) final</div><div class="ttdoc">reject 将任务抛弃 </div><div class="ttdef"><b>Definition:</b> threadpoolexecutor.hpp:364</div></div>
<div class="ttc" id="classWorkStealingPoolExecutor_html"><div class="ttname"><a href="classWorkStealingPoolExecutor.html">WorkStealingPoolExecutor</a></div><div class="ttdoc">任务窃取线程池,从下一个线程的任务队列窃取任务 </div><div class="ttdef"><b>Definition:</b> workstealingpoolexecutor.hpp:9</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者 &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
